#include "VDUser.h"

#include <QDateTime>
#include <QDataStream>

QString makeID(const QString &name)
{
	return name + '|' + QDateTime::currentDateTime().toString("yy.dd.MM/hh:mm:ss.zzz");
}

//////////////////////////////////////////////////////////////////////////
// static
VDUser* VDUser::makeNew( const QString &name )
{
	if(name.isEmpty())
		return NULL;

	return new VDUser(makeID(name), name);
}

VDUser* VDUser::makeFromData( const QByteArray& data )
{
	QString ID;
	QString name;
        //quint16 checkSum;

	QDataStream stream(data);

	if (stream.atEnd())
		return NULL;

	stream >> ID;

	if (stream.atEnd())
		return NULL;

	stream >> name;

	if (!stream.atEnd())
		return NULL;

	if (ID.isEmpty() || name.isEmpty())
		return NULL;

	return new VDUser(ID, name);
}
// 
// VDUser* VDUser::makeFromSettings()
// {
// 
// }

//////////////////////////////////////////////////////////////////////////
//
VDUser::VDUser( const QString &ID, const QString &name )
{
	m_ID = ID;
	m_name = name;
	makeData();

	m_state = VD::Offline;
	m_countAnswer = 0;
}

void VDUser::makeData()
{
	m_data.clear();
	QDataStream stream(&m_data, QIODevice::WriteOnly);

	stream << m_ID;
	stream << m_name;

	m_checkSum = qChecksum(m_data.data(), m_data.size());
}

void VDUser::setName( const QString &name )
{
	if (name.isEmpty())
		return;

	m_name = name;
	makeData();
}
